<?php


namespace app\api\controller\v1;


use app\api\model\BikeCodeScanDistMapNew;
use app\api\model\BikeDispatchModel;
use app\api\model\BikeMgr;
use app\api\model\BikeModel;
use app\api\model\BikeOperRecordModel;
use app\api\model\BikeTrackModelNEW;
use app\api\model\BikeWarnModel;
use app\api\model\EcuLogModelNew;
use app\api\model\EventNoticeReceiverModel;
use app\api\model\ParkModel;
use app\api\model\StatNewModel;
use app\api\model\UserModel;
use app\common\controller\UserBaseController;
use app\common\library\AMapModel;
use app\common\library\DLock;
use app\common\library\IDMan;
use app\common\library\StatModel;
use app\common\library\XiaoAnEcuModel;
use common;
use ApiOper;
use think\Db;
use think\Exception;

class BikeController extends UserBaseController
{

    function alert_state_to_string($alert_state)
    {
        $enums = [
            BIKE_ALERT_LOCK_FAILED => '关锁失败',
            BIKE_ALERT_UNDER_VOLT => '欠压',
            BIKE_ALERT_OFFLINE => '离线',
            BIKE_ALERT_GPS_FAILED => '定位失败',
            BIKE_ALERT_FAULT => '故障警告',
            BIKE_ALERT_STOLEN => '怀疑被盗',
            BIKE_ALERT_EX_UNLOCK => '异常开锁',
            BIKE_ALERT_LONG_BATTERY_UNLOCKED => '电池锁长时间打开',
            BIKE_ALERT_LONG_BATTERY_OFF => '长时间电池空置',
            BIKE_ALERT_EX_BATTERY_OFF => '电池异常空置',
            BIKE_ALERT_EX_LONG_UNLOCK => '超时未锁',
            BIKE_ALERT_LONG_NO_SCANED => '长时间无用户扫码',
            BIKE_ALERT_LONG_NO_GPS => '长时间无定位信息',
            BIKE_ALERT_LONG_NO_BUS => '长时间未被骑行',
        ];


        $s = [];
        foreach ($enums as $k => $v) {
            if (($alert_state & $k) > 0) {
                $s[] = $v;
            }
        }
        if (empty($s)) {
            $s = "正常";
        } else {
            $s = implode(",", $s);
        }
        return $s;
    }

    function infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'optstring',
            'bike_sn' => 'optstring',
            'user_coordinate' => ['optarray', null],
            'auto' => ['optint',0],
        );

        $this->check_params();
        $this->check_token();
        $bike_info = null;
        $scan = false;
        if (isset($this->argv['id']) && !empty($this->argv['id'])) {
            $bike_info = (new BikeModel())->get_info_by_id($this->argv['id']);
        } else if (isset($this->argv['bike_sn'])) {
            $bike_info = (new BikeModel())->get_info_by_sn($this->argv['bike_sn']);
            $this->resp['info'] = $bike_info;
            $scan = true;
        }

        if (!empty($bike_info)) {
            if ($scan && ApiOper::instance()->userinfo()['user_type'] == OPER_TYPE_USER) {
                (new BikeOperRecordModel())->add(BIKE_OPER_TYPE_SCAN_BIKE_TDCODE, $bike_info['city_id'], $bike_info['id'], null, null);
                if ($bike_info['alert_state'] & BIKE_ALERT_LONG_NO_BUS) {
                    //失联车辆,报告
                    EventNoticeReceiverModel::instance()->send_sms_miss_bike_scaned($bike_info['city_id'], $bike_info['sn']);
                }

                if ($bike_info['is_low_battery']) {
                    BikeModel::instance()->incr_low_battery_scaned_count($bike_info['id']);
                }
                if (!empty($this->argv['bike_sn'])) {
                    BikeModel::instance()->user_scaned($bike_info['id']);
                    $invalid = false;
                    if ($bike_info['health_state'] != 0 || $bike_info['is_low_battery'] == 1 || $bike_info['park_state'] == BIKE_PARK_STAT_OSA) {
                        $invalid = true;
                    }

                    (new StatModel())->user_scan_incr($bike_info['city_id'], $invalid);
                    (new StatNewModel())->tdcode_scaned_incr($bike_info['city_id'], $invalid, date('Y-m-d'));
                }

                if ($bike_info['bus_state'] != 0){
                    if ($bike_info['last_order_oper_id'] != ApiOper::instance()->userinfo()['id']){
                        $bike_info['bluetooth_name'] = null;
                        $bike_info['bluetooth_token'] = null;
                    }
                }
            }
            if ($this->argv['user_coordinate']) {
                (new BikeCodeScanDistMapNew())->add($bike_info['city_id'], $this->argv['user_coordinate'], date('Y-m-d H:i:s'));

                if (isset($this->argv['auto'])){
                    if($this->argv['auto'] == 1){
                        $info =[
                            'sn' => $bike_info['sn'],
                            'last_scaned_user_id' => ApiOper::instance()->userinfo()['id'],
                        ];
                        (new BikeCodeScanDistMapNew())->add_scanned_log($bike_info['city_id'],$info);
                    }
                }
            }
            (new BikeMgr())->on_bike_info_update($bike_info['imei'], []);
        }

        if ($bike_info) {
            $bike_info['alert_state_desc'] = $this->alert_state_to_string($bike_info['alert_state']);
        } else {
            $this->throw_myexception(X_ERROR, "无效的车辆编号!");
        }

        if ($bike_info['inv_state'] != BIKE_INV_STATE_INFO_INIT && $bike_info['inv_state'] != BIKE_INV_STATE_DELIVING) {
            if (ApiOper::instance()->userinfo()['user_type'] == OPER_TYPE_OPS || ApiOper::instance()->userinfo()['user_type'] == OPER_TYPE_CC) {
                if ($bike_info['city_id'] != ApiOper::instance()->userinfo()['city_id']) {
                    $bike_info = null;
                }
            }
        }

        if (ApiOper::instance()->userinfo()['user_type'] == OPER_TYPE_USER) {
            $bike_info = BikeModel::user_filter($bike_info);
        }

        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询查询信息失败，请重试！");
        }

        $this->resp['info'] = $bike_info;
    }

    function coord_and_park_nearbyAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
        );

        $this->check_params();
        $this->check_token();
        $coordinate = BikeModel::instance()->get_coordinate_by_id($this->argv['bike_id']);


        $parks = (new ParkModel())->query_list_near($this->city_id, $coordinate);

        $this->resp['coordinate'] = $coordinate;
        $this->resp['parks'] = $parks;
    }

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'optstring',
            'sim_state' => 'optint',
            'inv_state' => 'optint',
            'bus_state' => 'optint',
            'health_state' => 'optint',
            'is_in_no_parking_area' => 'optint',
            'is_in_service_area' => 'optint',
            'battery_level_upper' => 'optint',
            'is_wheel_locked' => 'optint',
            'start_time' => 'optstring',
            'end_time' => 'optstring',
            'sk' => 'optstring',
            'model' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'flush' => ['optint', 0],
            'time_interval_type' => 'optint',
            'start_time' => 'optdatetime',
            'end_time' => 'optdatetime',
            'park_id' => 'optstring',
            'all_city' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();


        $operinfo = ApiOper::instance()->userinfo();
        $city_id = $this->city_id;
        if ($this->argv['all_city'] && $this->argv['all_city'] == 1) {
            if ($operinfo['user_type'] == OPER_TYPE_CC && $operinfo['role_id'] == STAFF_ROLE_SUPER_ADMIN) {
                $city_id = null;
            }
        }

        $list = (new BikeModel())->get_list($city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;

        $this->resp['total'] = count($list);//hisql::query_count();
        if ($this->argv['flush']) {
            foreach ($list as $e) {
                (new BikeModel())->save_to_mongo($e);
            }
        }
    }

    function list_by_skAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();

        $list = [];
        $total = 0;
        if (!empty($this->argv['sk'])) {
            $list = (new BikeModel())->get_list(null, $this->argv, $this->argv['pos'], $this->argv['limit']);
        }
        if (count($list)) {
            $total = count($list);
        }


        $this->resp['list'] = $list;
        $this->resp['total'] = $total;
    }

    function voiceAction()
    {
        $this->paramlist = array(
            'city_id' => 'string',
            'bike_id' => 'string',
            'type' => 'int',
            'bound_order_info' => 'array',
        );

        $this->check_params();
        $dlock = new DLock("DEVICE_VOICE_" . $this->argv['bike_id'], 60);
        $bikeinfo = (new BikeModel())->get_info_by_id($this->argv['bike_id']);
        if (empty($bikeinfo)) {
            $this->throw_myexception(X_ERROR, "get bike info failed!");
        }


        (new BikeMgr())->device_voice($bikeinfo['imei'], $this->argv['type'], $this->argv['bound_order_info']);
    }

    function c2aAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'bike_id' => 'string',
            'coordinate' => 'optarray',
        );

        $this->check_params();

        $coordinate = null;
        if (isset($this->argv['coordinate'])) {
            $coordinate = $this->argv['coordinate'];
        } else {
            $bikeinfo = (new BikeModel())->get_info_by_id($this->argv['bike_id']);
            if (empty($bikeinfo)) {
                $this->throw_myexception(X_ERROR, "get bike info failed!");
            }

            $coordinate = $bikeinfo['coordinate'];
        }

        $address = (new AMapModel())->map_c_2_a($coordinate);
        if ($address) {
            (new BikeMgr())->update_address($this->argv['city_id'], $this->argv['bike_id'], $address);
        }
    }

    function unlockAction()
    {
        $this->paramlist = array(
            'bike_id' => 'string',
            'bound_order_type' => ['optstring', null],
            'bound_order_id' => ['optstring', null],
            'bound_order_op' => ['optstring', ''],
            'user_type' => ['optint', OPER_TYPE_CC],
            'user_id' => ['optstring', 'SYSTEM'],
            'user_coordinate' => ['optfloatarray', [0, 0]],
        );

        $this->check_params();
        $this->check_token();


        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败！");
        }
        if (!in_array($bike_info['bus_state'], [BIKE_BUS_STAT_RIDING,BIKE_BUS_STAT_USER_REPARK, BIKE_BUS_STAT_OPER_REPARK])) {
            $this->throw_myexception(X_ERROR, "bus_state = {$bike_info['bus_state']}！");
        }

        $defend_update_time = 0;
        if (!empty($bike_info['defend_update_time'])) {
            $defend_update_time = strtotime($bike_info['defend_update_time']);
        }

        //hilog::debug($bike_info['id'], "is_defend_on=" . $bike_info['is_defend_on'], $bike_info['defend_update_time']);
        if ($bike_info['is_defend_on'] == 0 && time() - $defend_update_time < 20) {
            $this->throw_myexception(X_ERROR, "车辆撤防，{$bike_info['defend_update_time']}");
        }

        $bound_order_op = $this->argv['bound_order_op'];
        if (empty($bound_order_op)) {
            $bound_order_op = "运维开锁";
        }

        $bound_order_info = [
            'order_type' => $this->argv['bound_order_type'],
            'order_id' => $this->argv['bound_order_id'],
            'op' => $bound_order_op,
        ];

        (new BikeMgr())->unlock($bike_info['city_id'], $bike_info['id'], $bound_order_info);
    }

    function lockAction()
    {
        $this->paramlist = array(
            'bike_id' => 'string',
            'bound_order_type' => ['optstring', ''],
            'bound_order_id' => ['optstring', ''],
            'bound_order_op' => ['optstring', ''],
            'user_type' => ['optint', OPER_TYPE_CC],
            'user_id' => ['optstring', 'SYSTEM'],
            'user_coordinate' => ['optfloatarray', 0, 0],
            'city_id' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败！");
        }

        if (in_array($bike_info['bus_state'], [BIKE_BUS_STAT_RIDING,BIKE_BUS_STAT_USER_REPARK,BIKE_BUS_STAT_OPER_REPARK])) {
            $this->throw_myexception(X_ERROR, "bus_state = {$bike_info['bus_state']}！");
        }

        $defend_update_time = 0;
        if (!empty($bike_info['defend_update_time'])) {
            $defend_update_time = strtotime($bike_info['defend_update_time']);
        }

        //hilog::debug($bike_info['id'], "is_defend_on=" . $bike_info['is_defend_on'], $bike_info['defend_update_time']);
        if ($bike_info['is_defend_on'] == 1 && time() - $defend_update_time < 20) {
            $this->throw_myexception(X_ERROR, "车辆设防，{$bike_info['defend_update_time']}");
        }
        $bound_order_op = $this->argv['bound_order_op'];
        if (empty($bound_order_op)) {
            $bound_order_op = "运维关锁";
        }

        $bound_order_info = [
            'order_type' => $this->argv['bound_order_type'],
            'order_id' => $this->argv['bound_order_id'],
            'op' => $bound_order_op,
        ];

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if ($bike_info['bus_state'] == BIKE_BUS_STAT_OPER_REPARK) {
            (new BikeMgr())->temp_lock($bike_info['city_id'], $this->argv['bike_id'], $bound_order_info);
        } else {
            (new BikeMgr())->lock($bike_info['city_id'], $this->argv['bike_id'], $bound_order_info);
        }
    }

    function acc_onAction()
    {
        $this->paramlist = array(
            'bike_id' => 'string',
            'bound_order_type' => ['optstring', null],
            'bound_order_id' => ['optstring', null],
            'bound_order_op' => ['optstring', 'OPER'],
            'user_type' => ['optint', OPER_TYPE_CC],
            'user_id' => ['optstring', 'SYSTEM'],
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败！");
        }

        if (!in_array($bike_info['bus_state'], [BIKE_BUS_STAT_RIDING,BIKE_BUS_STAT_USER_REPARK,BIKE_BUS_STAT_OPER_REPARK])) {
            $this->throw_myexception(X_ERROR, "bus_state = {$bike_info['bus_state']}！");
        }
        if ($bike_info['is_acc_on'] == 1) {
            $this->throw_myexception(X_ERROR, "acc on!");
        }

        $bound_order_info = [
            'order_type' => $this->argv['bound_order_type'],
            'order_id' => $this->argv['bound_order_id'],
            'op' => $this->argv['bound_order_op'],
        ];

        (new BikeMgr())->acc_on($bike_info['city_id'], $this->argv['bike_id'], $bound_order_info);
    }

    function acc_offAction()
    {
        $this->paramlist = array(
            'city_id' => 'string',
            'bike_id' => 'string',
            'bound_order_type' => ['optstring', null],
            'bound_order_id' => ['optstring', null],
            'bound_order_op' => ['optstring', 'OPER'],
            'user_type' => ['optint', OPER_TYPE_CC],
            'user_id' => ['optstring', 'SYSTEM'],
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if ($bike_info['is_acc_on'] == 0) {
            $this->throw_myexception(X_ERROR, "acc off!");
        }

        $bound_order_info = [
            'order_type' => $this->argv['bound_order_type'],
            'order_id' => $this->argv['bound_order_id'],
            'op' => $this->argv['bound_order_op'],
        ];
        (new BikeMgr())->acc_off($this->argv['city_id'], $this->argv['bike_id'], $bound_order_info);
    }

    function query_device_info_rtAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);

        (new BikeMgr())->query_bike_info_rt($bike_info['imei']);
        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);
        $this->resp['info'] = $bike_info;
    }

    function unlock_batteryAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
            'user_coordinate' => ['optfloatarray', [0, 0]],
            'bound_order_type' => ['optstring', null],
            'bound_order_id' => ['optstring', null],
            'bound_order_op' => ['optstring', '开电池锁'],

        );

        $this->check_params();
        $this->check_token();

        $bike_info = (new BikeModel())->get_info_by_id($this->argv['id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败！");
        }
        $bound_order_info = [
            'order_type' => $this->argv['bound_order_type'],
            'order_id' => $this->argv['bound_order_id'],
            'op' => $this->argv['bound_order_op'],
        ];
        $ret = (new BikeMgr())->battery_unlock($bike_info['city_id'], $bike_info['id'], $bound_order_info);
        $this->resp['status'] = $ret[0];
        if ($ret[0] != 0) {
            $this->resp['msg'] = $ret[1];
        }
    }

    function oper_listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'optstring',
            'type' => 'optint',
            'user_type' => 'optint',
            'user_id' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'bound_order_id' => 'optstring',
        );

        $this->check_params();
        $this->check_token();
        $city_id = $this->city_id;
        if (isset($this->argv['bike_id'])) {
            $bike_info = (new BikeModel())->get_info_by_id($this->argv['bike_id']);
            $city_id = $bike_info['city_id'];
        }

        if (isset($this->argv['user_id'])) {
            $userinfo = (new UserModel())->query_info($this->argv['user_id']);
            if ($userinfo && isset($userinfo['city_id'])) {
                $city_id = $userinfo['city_id'];
            } else {
                $this->resp['list'] = [];
                $this->resp['total'] = 0;
                return;
            }
        }

        $list = (new BikeOperRecordModel())->query_list($city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = hisql::query_count();
    }

    function maintainAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'user_coordinate' => 'floatarray',
        );

        $this->check_params();
        $this->check_token();

        (new BikeMgr())->maintain($this->city_id, $this->argv['bike_id']);
    }

    function checkAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'user_coordinate' => 'floatarray',
        );

        $this->check_params();
        $this->check_token();

        (new BikeMgr())->check($this->city_id, $this->argv['bike_id']);
    }

    function list_maintainAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'is_maintained' => 'int',
            "sk" => 'optstring',
            'pno' => 'optint',
            'psize' => 'optint',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_maintain($this->argv);
        $this->resp['list'] = $list;
        $this->resp['total'] = hisql::query_count();
    }

    function list_invAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'inv_state' => 'optint',
            'start_time' => 'optdatetime',
            'end_time' => 'optdatetime',
            "sk" => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_inv($this->argv);
        $this->resp['list'] = $list;
        if ($this->argv['pos'] == 0) {
            $this->resp['total'] = hisql::query_count();
        }
    }

    function list_monitorAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'use_state' => 'optint',
            'is_defend_on' => 'optint',
            'battery_level_interval' => 'optintarray',
            'inv_state' => 'optint',
            'alert_state' => 'optint',
            'park_state' => 'optint',
            'health_state' => 'optint',
            'model' => 'optstring',
            "sk" => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_monitor($this->argv);
        $this->resp['list'] = $list;
        if ($this->argv['pos'] == 0) {
            $this->resp['total'] = hisql::query_count();
        }
    }

    function list_alertAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'start_time' => 'optdatetime',
            'end_time' => 'optdatetime',
            'duration_interval' => 'optintarray',
            'alert_state' => 'optint',
            'sk' => 'optstring',
            'reparkflag' => 'optint'
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_alert($this->argv);
        $this->resp['list'] = $list;
        if ($this->argv['pos'] == 0) {
            $this->resp['total'] = hisql::query_count();
        }
    }

    function list_warnAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'sk' => 'optstring',
            'reparkflag' => 'optint'
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_warn($this->argv);
        $this->resp['list'] = $list;
        if ($this->argv['pos'] == 0) {
            $this->resp['total'] = hisql::query_count();
        }
    }

    function list_low_batteryAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'low_battery_scaned_count_interval' => 'optintarray',
            "sk" => 'optstring',
            'model' => 'optstring',
            'pno' => ['optint', 1],
            'psize' => ['optint', 30],
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_low_battery($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        if (!empty($list)) {
            $this->resp['total'] = hisql::query_count();
        } else {
            $this->resp['total'] = 0;
        }
    }

    function list_missAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'duration_interval' => 'optintarray',
            'battery_level_interval' => 'optintarray',
            'alert_state' => 'optint',
            "sk" => 'optstring',
            'order_state' => 'optint',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_miss($this->argv);
        $this->resp['list'] = $list;
        if (!empty($list)) {
            $this->resp['total'] = hisql::query_count();
        } else {
            $this->resp['total'] = 0;
        }
    }

    function list_stolenAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'duration_interval' => 'optintarray',
            'battery_level_interval' => 'optintarray',
            'alert_state' => 'optint',
            "sk" => 'optstring',
            'order_state' => 'optint',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_miss($this->argv);
        $this->resp['list'] = $list;
        if (!empty($list)) {
            $this->resp['total'] = hisql::query_count();
        } else {
            $this->resp['total'] = 0;
        }
    }

    function list_alert_faultAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            "sk" => 'optstring',
            'order_state' => 'optint',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeModel())->query_list_alert_fault($this->argv);
        $this->resp['list'] = $list;
        if (!empty($list)) {
            $this->resp['total'] = hisql::query_count();
        } else {
            $this->resp['total'] = 0;
        }
    }

    function list_to_ride_nearbyAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'optstring',
            'coordinate' => 'intarray',
            'distance' => ['optint', 10000],
            'flag' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            'health_state' => 0,
            'battery_level' => ['$gt' => 5],
            'bus_state' => 0,
            'inv_state' => 0,
        ];

        $list = (new BikeModel())->query_list_nearby($this->city_id, $this->argv['coordinate'], $this->argv['distance'], $cd);
        $this->resp['list'] = $list;
        if ($this->argv['flag']) {
            $this->resp['parks'] = (new ParkModel())->query_list_near($this->city_id, $this->argv['coordinate']);
        }
    }

    function list_to_repark_nearbyAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'coordinate' => 'intarray',
            'distance' => ['optint', 5000],
            'repark_index' => 'optint',
            'flag' => ['optint', 0],
            'park_state' => 'optint',
            'repark_flag' => ['optint',0],
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            //  'health_state' => 0,
            'bus_state' => 0,
        ];

        if (isset($this->argv['repark_index']) && $this->argv['repark_index'] > 0) {
            if($this->argv['repark_index']>=1 && $this->argv['repark_index'] <= 4){
                $cd['repark_index'] = ['$gte' => $this->argv['repark_index'],'$lte' =>4];
            }
            else
                $cd['repark_index'] = $this->argv['repark_index'];
        } else {
            // $cd['repark_index'] = ['$gt' => 0];
        }
        if (isset($this->argv['park_state'])) {
            $cd['park_state'] = $this->argv['park_state'];
        }

        $list = (new BikeModel())->query_list_nearby($this->city_id, $this->argv['coordinate'], $this->argv['distance'], $cd);
        $this->resp['list'] = $list;
        if ($this->argv['flag']) {
            $this->resp['parks'] = (new ParkModel())->query_list_near($this->city_id, $this->argv['coordinate']);
        }

        if($this->argv['repark_flag'] >0){
            $this->resp['repark_count'] = (new BikeModel())->query_list_repark_count($this->city_id);
        }
    }

    function list_to_change_battery_nearbyAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'coordinate' => 'intarray',
            'distance' => ['optint', 5000],
            'battery_level_max' => ['optint', 35],
            'is_under_volt' => 'optint',
            'flag' => ['optint', 0],
            'battery_flag' => ['optint',0],
            'model' => ['optstring','']
        );

        $this->check_params();
        $this->check_token();

        $battery_level_max = 35;
        if ($this->argv['battery_level_max'] < $battery_level_max) {
            $battery_level_max = $this->argv['battery_level_max'];
        }
        if ($battery_level_max == 0) {
            $battery_level_max = 0.01;
        }

        if ($battery_level_max == 35 && $this->city_id == '520200'){
            $battery_level_max = 50;
        }

        $cd = [
            //'health_state' => 0,
            'battery_level' => ['$lt' => $battery_level_max],
            'bus_state' => 0,
        ];

        if (isset($this->argv['is_under_volt'])) {
            $cd['is_under_volt'] = $this->argv['is_under_volt'];
        }

        if (!empty($this->argv['model'])){
            $cd['model'] = $this->argv['model'];
        }

        $list = (new BikeModel())->query_list_nearby($this->city_id, $this->argv['coordinate'], $this->argv['distance'], $cd);
        $this->resp['list'] = $list;


        if ($this->argv['flag']) {
            $this->resp['parks'] = (new ParkModel())->query_list_near($this->city_id, $this->argv['coordinate']);
        }

        if ($this->argv['battery_flag']) {
            $this->resp['battery_count'] = (new BikeModel())->query_list_change_battery($this->city_id,$this->argv['model']);
        }
    }

    function list_to_repair_nearbyAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'coordinate' => 'intarray',
            'distance' => ['optint', 10000],
            'inv_state' => 'optint',
            'flag' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            'health_state' => ['$ne' => 0],
        ];

        if (isset($this->argv['inv_state'])) {
            $cd['inv_state'] = $this->argv['inv_state'];
        }

        $list = (new BikeModel())->query_list_nearby($this->city_id, $this->argv['coordinate'], $this->argv['distance'], $cd);
        $this->resp['list'] = $list;
        if ($this->argv['flag']) {
            $this->resp['parks'] = (new ParkModel())->query_list_near($this->city_id, $this->argv['coordinate']);
        }
    }

    function list_to_maintain_nearbyAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'coordinate' => 'floatarray',
            'distance' => ['optint', 10000],
            'model' => 'optstring',
            'flag' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            'last_maintain_time' => ['$lt' => date('Y-m-d H:i:s', strtotime('-1 month'))],
        ];

        if (isset($this->argv['model'])) {
            $cd['model'] = $this->argv['model'];
        }

        $list = (new BikeModel())->query_list_nearby($this->city_id, $this->argv['coordinate'], $this->argv['distance'], $cd);
        $this->resp['list'] = $list;
        if ($this->argv['flag']) {
            $this->resp['parks'] = (new ParkModel())->query_list_near($this->city_id, $this->argv['coordinate']);
        }
    }

    function refresh_infoAction()
    {
        $this->paramlist = [
            'bike_sn' => 'optstring',
            'id' => 'optstring',
        ];
        $this->check_params();
        $this->check_token();

        $bike_info = null;
        if (isset($this->argv['id'])) {
            $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);
        } else if (isset($this->argv['bike_sn'])) {
            $bike_info = BikeModel::instance()->get_info_by_sn($this->argv['bike_sn']);
        }

        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败!");
        }

        $ret = (new BikeMgr())->query_bike_info_rt($bike_info['imei']);
        if (empty($ret)) {
            $this->throw_myexception(X_ERROR, "更新车辆信息失败!");
        }

        if (isset($this->argv['id'])) {
            $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);
        } else if (isset($this->argv['bike_sn'])) {
            $bike_info = BikeModel::instance()->get_info_by_sn($this->argv['bike_sn']);
        }
        $this->resp['info'] = $bike_info;
    }

    function query_trackAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'sn' => 'string', //兼容 ecu_sn, bike_sn, imei
            'start_time' => 'datetime',
            'end_time' => 'datetime',
            'from_xiao_an' => ['optint', 1],
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_sn($this->argv['sn']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败！");
        }
        $track = null;

        if ($this->argv['from_xiao_an']) {
            $track = (new XiaoAnEcuModel())->get_track($bike_info['imei'], $this->argv['start_time'], $this->argv['end_time']);
        } else {
            $track = (new BikeTrackModelNEW())->query_track($bike_info['imei'], $this->argv['start_time'], $this->argv['end_time'], 0, 10000);
        }
        $this->resp['track'] = $track;
    }

    function query_trackxAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'sn' => 'string',
            'start_time' => 'datetime',
            'end_time' => 'datetime',
            'fields' => 'stringarray',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_sn($this->argv['sn']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败！");
        }

        $track = (new BikeTrackModelNEW())->query($bike_info['imei'],
            $this->argv['fields'],
            $this->argv['start_time'],
            $this->argv['end_time'],
            0, 10000);

        $this->resp['track'] = $track;
    }

    function ecu_callback_logsAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'sn' => 'string',
            'start_time' => 'datetime',
            'end_time' => 'datetime',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = (new BikeModel())->get_info_by_sn($this->argv['sn']);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆失败！");
        }

        $start_time = $this->argv['start_time'];
        $end_time = $this->argv['end_time'];
        if (strtotime($end_time) - strtotime($start_time) > 3600 * 12) {
            //$start_time = date('Y-m-d H:i:s', strtotime($end_time) - 3600 * 12);
        }

        $sn = ($bike_info['ecu_model'] == ECU_MODEL_TBIT_209D ? $bike_info['ecu_sn'] : $bike_info['imei']);
        $logs = (new EcuLogModelNew())->query($sn, $start_time, $end_time, $this->argv['pos'], $this->argv['limit']);

        $this->resp['list'] = $logs;
        $this->resp['total'] = (new EcuLogModelNew())->count($sn, $start_time, $end_time);
    }

    function ringAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'id' => 'string',
        );
        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败！");
        }
        $bound_order_info = [
            'order_type' => null,
            'order_id' => null,
            'op' => '寻车铃',
        ];
        (new BikeMgr())->device_voice($bike_info['imei'], ECU_VOICE_TYPE_RING, $bound_order_info);
    }

    //用户寻车铃
    function look_bike_ringAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'id' => 'string',
            'user_coordinate' => ['optarray', null],
        );
        $this->check_params();
        $this->check_token();

        $l = new DLock($this->argv['id'], 5);

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败！");
        }

        //判断距离
        if (isset($this->argv['user_coordinate'])){
            if (!empty($this->argv['user_coordinate'])){
                $dis = common::distance($bike_info['coordinate'],$this->argv['user_coordinate']);
                if($dis <500){
                    $bound_order_info = [
                        'order_type' => null,
                        'order_id' => null,
                        'op' => '用户寻车铃',
                    ];
                    (new BikeMgr())->device_voice($bike_info['imei'], ECU_VOICE_TYPE_RING, $bound_order_info);
                }
            }
        }

    }

    function xiaoan_code_to_msg($code)
    {
        $code_eror_msg = [
            100 => 'ECU服务器内部错误',
            101 => '请求无IMEI',
            103 => '无请求内容',
            104 => '请求URL错误',
            105 => '请求范围过大',
            106 => '接入服务器无响应',
            107 => '接入服务器不在线',
            108 => '设备无响应',
            109 => '设备离线',
            132 => '车辆行驶中，不执行锁车',
        ];
        if (isset($code_eror_msg[$code])) {
            return $code_eror_msg[$code];
        }
        return '未知错误';
    }

    function ring_testAction()
    {
        $imei = '869270049333247';
        (new XiaoAnEcuModel())->device_voice($imei, 8);
    }

    function testAction()
    {
        $this->paramlist = [
            'id' => 'string'
        ];


        $this->check_params();
        $this->check_token();


        $l = new DLock($this->argv['id'], 60);

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败！");
        }

        if ($bike_info['inv_state'] != BIKE_INV_STATE_INFO_INIT) {
            $this->throw_myexception(X_ERROR, "车辆已投放，不能测试！");
        }

        $bound_order_info = [
            'order_type' => null,
            'order_id' => null,
            'op' => '测试',
        ];

        $ret = (new BikeMgr())->unlock($this->city_id, $bike_info['id'], $bound_order_info);
        if ($ret[0] != 0) {
            $this->throw_myexception(ERROR_LOCK_ERROR, "开锁失败：" . $ret[1]);
        }

        sleep(3);

        $ret = (new BikeMgr())->lock($this->city_id, $bike_info['id'], $bound_order_info);
        if ($ret[0] != 0) {
            $this->throw_myexception(ERROR_LOCK_ERROR, "关锁失败：" . $ret[1]);
        }

        sleep(2);
        $ret = (new BikeMgr())->battery_unlock($this->city_id, $bike_info['id'], $bound_order_info);
        if ($ret[0] != 0) {
            $this->throw_myexception(ERROR_LOCK_ERROR, "开电池锁失败：" . $ret[1]);
        }
        //   (new BikeMgr())->device_voice($bike_info['imei'], XiaoAnEcuModel::XIAO_AN_VOICE_TYPE_RING, $bound_order_info);

        sleep(2);
        $ret = (new BikeMgr())->device_voice($bike_info['imei'], ECU_VOICE_TYPE_RING, $bound_order_info);
        if ($ret[0] != 0) {
            $this->throw_myexception(ERROR_LOCK_ERROR, "播放语音失败：" . $ret[1]);
        }
    }

    function bind_ecuAction()
    {
        $this->paramlist = [
            'imei' => 'string',
            'ecu_sn' => 'string',
            'token' => 'string',
            'bike_sn' => 'string',
        ];

        $this->check_params();
        $this->check_token();


        $lock = new DLock($this->argv['bike_sn'], 10);

        $bike_info = BikeModel::instance()->get_info_by_sn($this->argv['bike_sn']);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败!");
        }

        if (!empty($bike_info['imei'])) {
            $this->throw_myexception(X_ERROR, "车辆已经绑定ECU！");
        }


        $imei_bike_info = (new BikeModel())->get_info_by_imei($this->argv['imei']);
        if (!empty($imei_bike_info)) {
            $this->throw_myexception(X_ERROR, "ECU被其他车辆绑定！");
        }

        BikeModel::instance()->bind_ecu($bike_info['id'], $this->argv['imei'], $this->argv['ecu_sn']);
    }

    function change_ecuAction()
    {
        $this->paramlist = [
            'new_imei' => 'string',
            'new_ecu_sn' => 'string',
            'token' => 'string',
            'bike_sn' => 'string',
        ];

        $this->check_params();
        $this->check_token();


        $lock = new DLock($this->argv['bike_sn'], 10);

        $imei = $this->argv['new_imei'];

        $bike_sn = $this->argv['bike_sn'];
        $ecu_sn = $this->argv['new_ecu_sn'];
        if (strlen($ecu_sn) == 9) {
            $ecu_sn = $ecu_sn . "f";
        }


        $bike_info = BikeModel::instance()->get_info_by_sn($bike_sn);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败!");
        }

        $ecu_info = null;
        if (empty($imei)) {//泰比特条形码没有IMEI信息
            $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            if (empty($ecu_info)) {
                $ecu_info = [
                    'imei' => $ecu_sn,
                    'sn' => $ecu_sn,
                    'model' => ECU_MODEL_TBIT_209D,
                    'brand' => 'TBIT',
                ];
            }
        } else {
            if (strlen($imei) != strlen('866039042496290')) {
                $this->throw_myexception(X_ERROR, "IMEI格式错误!");
            }
            $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            if (empty($ecu_info)) {
                $ecu_info = [
                    'imei' => $imei,
                    'sn' => $ecu_sn,
                    'model' => ECU_MODEL_XIAOAN_ATMX3F,
                    'brand' => 'XIAOAN',
                ];
            }
        }

        if (empty($ecu_info)) {
            $this->throw_myexception(X_ERROR, "查询ecu信息失败!");
        }

        if ($ecu_info['model'] != $bike_info['ecu_model']) {
            $this->throw_myexception(X_ERROR, "ecu型号不匹配!");
        }

        $imei_bike_info = (new BikeModel())->get_info_by_imei($ecu_info['imei']);
        if (!empty($imei_bike_info)) {
            $this->throw_myexception(X_ERROR, "ECU被其他车辆绑定！");
        }

        BikeModel::instance()->bind_ecu($bike_info['id'], $ecu_info['imei'], $ecu_info['sn']);
        (new BikeMgr())->asyn_update_bluetooth($bike_info);
    }

    function change_snAction()
    {
        $this->paramlist = [
            'token' => 'string',
            'sk' => 'string',
            'new_sn' => 'string',
        ];

        $this->check_params();
        $this->check_token();

        $sk = $this->argv['sk'];
        $bike_info = null;
        if (IDMan::is_bike_imei($sk)) {
            $bike_info = BikeModel::instance()->get_info_by_imei($sk);
        } else if (IDMan::is_bike_sn($sk)) {
            $bike_info = BikeModel::instance()->get_info_by_sn($sk);
        }

        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败!");
        }

        $info = BikeModel::instance()->get_info_by_sn($this->argv['new_sn']);
        if (!empty($info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "车辆二维码已存在!");
        }

        BikeModel::instance()->change_sn($bike_info['id'], $this->argv['new_sn']);

        $bike_info['bluetooth_token'] = null;
        (new BikeMgr())->asyn_update_bluetooth($bike_info);
    }

    function unbind_ecuAction()
    {
        $this->paramlist = [
            'token' => 'string',
            'sk' => 'string',
        ];

        $this->check_params();
        $this->check_token();

        $sk = $this->argv['sk'];
        $bike_info = null;
        if (IDMan::is_bike_imei($sk)) {
            $bike_info = BikeModel::instance()->get_info_by_imei($sk);
        } else if (IDMan::is_bike_sn($sk)) {
            $bike_info = BikeModel::instance()->get_info_by_sn($sk);
        }


        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败!");
        }

        $lock = new DLock($bike_info['sn'], 10);

        if (empty($bike_info['imei'])) {
            $this->throw_myexception(X_ERROR, "车辆没有绑定ECU！");
        }

        BikeModel::instance()->unbind_ecu($bike_info['id']);
    }

    function testbleAction()
    {
        $sn = '08910134';
        $bike_info = BikeModel::instance()->get_info_by_sn($sn);
        $ret = (new XiaoAnEcuModel())->set_bluetooth($bike_info['imei'], 0x0a0a0505, $sn);
        $this->resp['ret'] = $ret;
        return;
        $ret = (new BikeTrackModelNEW())->query_track($bike_info['imei'], "2019-01-01 00:00:00", "2020-01-01 00:00:00", 0, 5);
        $this->resp['result'] = $ret;
    }


    function report_bluetooth_operAction()
    {
        $this->paramlist = array(
            'bike_id' => 'string',
            'bound_order_type' => ['optstring', ''],
            'bound_order_id' => ['optstring', ''],
            'bound_order_op' => ['optstring', ''],
            'user_coordinate' => ['optfloatarray', [0, 0]],
            'type' => 'int',
            'result' => 'array',
        );


        $this->check_params();
        $this->check_token();

        $bound_order_info = [
            'order_type' => $this->argv['bound_order_type'],
            'order_id' => $this->argv['bound_order_id'],
            'op' => $this->argv['bound_order_op'] . ' [蓝牙]',
        ];

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);

        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败！");
        }


        $info = [];
        if ($this->argv['type'] == BIKE_OPER_TYPE_UNLOCK && $this->argv['result']['success'] === 0) {
            $info = [
                'is_defend_on' => 0,
                'defend_update_time' => date('Y-m-d H:i:s'),
            ];
        } else if ($this->argv['type'] == BIKE_OPER_TYPE_LOCK && $this->argv['result']['success'] === 0) {
            $info = [
                'is_defend_on' => 1,
                'defend_update_time' => date('Y-m-d H:i:s'),
            ];
        }

        if(!isset($this->argv['result']['cost'])){
            $this->argv['result']['cost'] = 5;
        }

        $result = [
            $this->argv['result']['success'],
            $this->argv['result']['error_msg'],
            $this->argv['result']['cost'],
        ];
        (new BikeOperRecordModel())->add($this->argv['type'], $bike_info['city_id'], $bike_info['id'], $bound_order_info, $result, 'bluetooth');
        (new BikeMgr())->on_bike_info_update($bike_info['imei'], $info);
    }

    function report_bike_gpsAction()
    {
        $this->paramlist = [
            'bike_id' => 'string',
            'stat' => 'string',
            'gps' => 'string',
            'tm' => ['optint', 0],
        ];

        $this->check_params();
        //  $this->check_token();
        $this->check_token();


        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败！");
        }
        $statdata = $this->argv['stat'];

        $stat = [
            'model' => hexdec(substr($statdata, 4, 2)),
            'gsm' => hexdec(substr($statdata, 6, 2)),
            'sw' => hexdec(substr($statdata, 8, 2)),
            'voltage' => hexdec(substr($statdata, 10, 8)),
        ];
        $info['swinfo'] = BikeMgr::sw_to_info($stat['sw']);

        $gpsdata = $this->argv['gps'];

        $gps = [
            'timestamp' => hexdec(substr($gpsdata, 0 + 4, 8)),
            'longitude' => common::hex2float(substr($gpsdata, 8 + 4, 8)),
            'latitude' => common::hex2float(substr($gpsdata, 16 + 4, 8)),
            'speed' => hexdec(substr($gpsdata, 24 + 4, 2)),
            'course' => hexdec(substr($gpsdata, 26 + 4, 4)),
            'satellite' => 0,
            'hdop' => 0,
        ];

        $data = array_merge($stat, $gps);
        $timestamp = $this->argv['tm'];
        if ($timestamp == 0 || $timestamp > time()) {
            $timestamp = time();
        }

        (new BikeMgr())->on_xiaoan_ecu_event_gps($bike_info['imei'], $data, $timestamp, true);
        // (new EcuLogModel())->add($bike_info['city_id'],$bike_info['id'], json_encode($data));

        $log = [
            'event' => 10,
            'data' => $data,
            'imei' => $bike_info['imei'],
            'channel' => 'bluetooth',
        ];

        $sn = ($bike_info['ecu_model'] == ECU_MODEL_TBIT_209D ? $bike_info['ecu_sn'] : $bike_info['imei']);
        (new EcuLogModelNew())->add($sn, 10, $log);

        $this->resp['data'] = $info;
    }


    function check_defend_stateAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        $bike_info_rt = $bike_info;

        // $bike_info_rt = (new BikeMgr())->query_bike_info_rt($bike_info['imei']);
        // $bike_info = (new BikeModel())->get_info_by_id($this->argv['bike_id']);

        if ($bike_info['bus_state'] == BIKE_BUS_STAT_IDLE) {
            if ($bike_info_rt['is_defend_on'] === 0) {
                $bound_order_info = [
                    'order_type' => null,
                    'order_id' => null,
                    'op' => '检查-关锁',
                ];

                (new BikeMgr())->lock($bike_info['city_id'], $bike_info['id'], $bound_order_info);
            }
        } else if ($bike_info['bus_state'] == BIKE_BUS_STAT_RIDING) {
            if ($bike_info_rt['is_defend_on'] === 1) {
                $bound_order_info = [
                    'order_type' => ORDER_TYPE_USER_RIDE,
                    'order_id' => $bike_info['last_order_id'],
                    'op' => '检查骑行开锁',
                ];

                (new BikeMgr())->unlock($bike_info['city_id'], $bike_info['id'], $bound_order_info);
            }
        } else if ($bike_info['bus_state'] == BIKE_BUS_STAT_OPER_REPARK) {
            if ($bike_info_rt['is_defend_on'] === 1) {
                $bound_order_info = [
                    'order_type' => ORDER_TYPE_BIKE_REPARK,
                    'order_id' => $bike_info['last_repark_order_id'],
                    'op' => '检查挪车开锁',
                ];

                (new BikeMgr())->unlock($bike_info['city_id'], $bike_info['id'], $bound_order_info);
            }
        }
        else if ($bike_info['bus_state'] == BIKE_BUS_STAT_USER_REPARK) {
            if ($bike_info_rt['is_defend_on'] === 1) {
                $bound_order_info = [
                    'order_type' => ORDER_TYPE_USER_REPARK,
                    'order_id' => $bike_info['last_repark_order_id'],
                    'op' => '检查挪车开锁',
                ];

                (new BikeMgr())->unlock($bike_info['city_id'], $bike_info['id'], $bound_order_info);
            }
        }
        else if ($bike_info['bus_state'] == BIKE_BUS_STAT_TEMP_PARK) {
            if ($bike_info_rt['is_defend_on'] === 0) {
                $bound_order_info = [
                    'order_type' => ORDER_TYPE_BIKE_REPARK,
                    'order_id' => $bike_info['last_order_id'],
                    'op' => '临时停车关锁',
                ];

                (new BikeMgr())->lock($bike_info['city_id'], $bike_info['id'], $bound_order_info);
            }
        }
    }


    function vmsg_testAction()
    {
        $cd =[
            'city_id' => '450981',
            'ecu_brand' => 'TBIT',
        ];
        $list = BikeModel::instance()->query_list($cd);
        foreach ($list as $info){
            try {
                $data = [
                    'sn' => $info['ecu_sn'],
                    'imei' => $info['imei'],
                    'brand' => $info['ecu_brand'],
                    'model' => $info['ecu_model'],
                ];

                //hisql::insert('tb_ecu', $data);
                Db::table('tb_ecu')->insert($data);
            } catch (Exception $e) {
            }

        }
    }

    function complete_warnAction(){
        $this->paramlist = array(
            'bike_sn' =>'string',
            'com_staff_type' => 'int',
            'com_staff_id'   => 'string',
            'com_staff_name'   => 'string',
        );

        $this->check_params();

        $bike_info = BikeModel::instance()->get_info_by_sn($this->argv['bike_sn']);
        if($bike_info)
        {
            if (isset($bike_info['is_dispatched']) && $bike_info['is_dispatched'] != 0) {
                (new BikeDispatchModel())->complete_stask($this->argv['bike_sn'],$this->argv);
            }
        }

        (new BikeWarnModel())->complete_task($this->argv['bike_sn'],$this->argv);

    }
}
